﻿#!/usr/bin/env python
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
#   (c) 2007 K.Danilov aka koder
#   distributed under GNU GPL v2.0 - see
#   http://www.gnu.org/licenses/gpl.html for full licens text
#   this file is part of plazyn python library
#
#   This program is free software; you can redistribute it and/or
#   modify it under the terms of the GNU General Public License
#   as published by the Free Software Foundation; (using version 2 of licence)
#    
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#   
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#-------------------------------------------------------------------------------
import unittest
import lazy
import urllib2
#-------------------------------------------------------------------------------
class Cls(object):
    def __init__(self):
        self.called = False
#-------------------------------------------------------------------------------
def func(obj):
    obj.called = True
    return obj
#-------------------------------------------------------------------------------
atom_objects = [1,1.0,"asdasf",None,type(None)]
cobjects     = [[1,2],(2,(None,3)),{12:2}]
user_objects = [urllib2.Request,urllib2.Request("http://www.google.com"),Cls()]
#-------------------------------------------------------------------------------
class TestSequenceFunctions(unittest.TestCase):
    def testnslazy(self):
        lfunc = lazy.nslazy_dec(func)
        obj = Cls()
        res = lfunc(obj)
        self.assert_( not obj.called )
        self.assertRaises(AttributeError,object.__getattribute__,res,'v')
        self.assert_( res.v is obj )
        self.assert_( obj.called )
    def testMimiclazy(self):
        lfunc = lazy.mimiclazy_dec(func)
        obj = Cls()
        res = lfunc(obj)
        self.assert_( not obj.called )
        self.assert_( res is not obj )
        str(res)
        self.assert_( obj.called )
        self.assert_( res is not obj )
        #-----------------------------------------------------------------------
        lfunc = lazy.mimiclazy_dec(lambda x : x)
        #-----------------------------------------------------------------------
        obj = 1
        res = lfunc(1)
        self.assert_( res == obj )
        self.assert_( res + 1 == obj + 1)
        res += (((( res + 1 ) * 32 ) ** 3 ) / 5 ) % 239
        obj += (((( obj + 1 ) * 32 ) ** 3 ) / 5 ) % 239
        self.assert_( res  == obj )
#-------------------------------------------------------------------------------
if __name__ == '__main__':
    unittest.main()
#-------------------------------------------------------------------------------
